package com.softeem.music_sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.comm.QiniuOssUtils;
import com.softeem.music_sys.entity.Admin;
import com.softeem.music_sys.mapper.AdminMapper;
import com.softeem.music_sys.service.AdminService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Date;

/**
 * 管理员Service实现类
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    
    @Override
    public boolean register(Admin admin) {
        // 检查用户名是否已存在
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username", admin.getUsername());
        Admin existAdmin = this.getOne(wrapper);
        
        if (existAdmin != null) {
            return false; // 用户名已存在
        }
        
        // 设置创建时间
        admin.setCreateTime(new Date());
        admin.setStatus(1); // 默认启用
        
        return this.save(admin);
    }
    
    @Override
    public Admin login(String username, String password) {
        // 查询管理员
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        Admin admin = this.getOne(wrapper);
        
        // 检查是否存在
        if (admin == null) {
            return null;
        }
        
        // 检查密码
        if (!admin.getPassword().equals(password)) {
            return null;
        }
        
        // 检查状态
        if (admin.getStatus() != null && admin.getStatus() == 0) {
            return null;
        }

        
        // 不返回密码
        admin.setPassword(null);
        return admin;
    }
    
    @Override
    public Result registerAdmin(Admin admin) {
        boolean result = register(admin);
        return result ? Result.success().setMessage("注册成功") : Result.fail("注册失败");
    }
    
    @Override
    public Result loginAdmin(String username, String password) {
        Admin admin = login(username, password);
        if (admin != null) {
            return Result.success().setMessage("登录成功").setData(admin);
        } else {
            return Result.fail("用户名或密码错误");
        }
    }
    
    @Override
    public Result getAdminById(Integer id) {
        Admin admin = this.getById(id);
        if (admin != null) {
            // 不返回密码
            admin.setPassword(null);
            return Result.success().setData(admin);
        } else {
            return Result.fail("管理员不存在");
        }
    }
    
    @Override
    public Result updateAdmin(Admin admin) {
        boolean result = this.updateById(admin);
        return result ? Result.success().setMessage("更新成功") : Result.fail("更新失败");
    }
    
    @Override
    public Result deleteAdmin(Integer id) {
        boolean result = this.removeById(id);
        return result ? Result.success().setMessage("删除成功") : Result.fail("删除失败");
    }
    
    @Override
    public Result getAdminList(Integer page, Integer limit) {
        Page<Admin> pageUtil = new Page<>(page, limit);
        Page<Admin> resultPage = this.page(pageUtil);
        // 移除密码
        resultPage.getRecords().forEach(admin -> admin.setPassword(null));
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result searchAdmin(String username, Integer page, Integer limit) {
        Page<Admin> pageUtil = new Page<>(page, limit);
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.like("username", username);
        Page<Admin> resultPage = this.page(pageUtil, wrapper);
        // 移除密码
        resultPage.getRecords().forEach(admin -> admin.setPassword(null));
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result updateAdminStatus(Integer id, Integer status) {
        Admin admin = this.getById(id);
        if (admin != null) {
            admin.setStatus(status);
            boolean result = this.updateById(admin);
            return result ? Result.success().setMessage("状态更新成功") : Result.fail("状态更新失败");
        } else {
            return Result.fail("管理员不存在");
        }
    }
    
    @Override
    public Result uploadMusicFile(MultipartFile file) {
        try {
            InputStream in = file.getInputStream();
            String filename = file.getOriginalFilename();
            
            // 生成唯一文件名
            String uniqueFilename = System.currentTimeMillis() + "_" + filename;
            
            QiniuOssUtils utils = new QiniuOssUtils();
            String url = utils.upload(in, uniqueFilename);
            
            if (url != null) {
                return Result.success().setMessage("上传成功").setData(url);
            } else {
                return Result.fail("上传失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("上传异常: " + e.getMessage());
        }
    }
    
    @Override
    public Admin getAdminByIdInternal(Integer id) {
        return this.getById(id);
    }
    
    @Override
    public boolean updateAdminInternal(Admin admin) {
        return this.updateById(admin);
    }
    
    @Override
    public boolean deleteAdminInternal(Integer id) {
        return this.removeById(id);
    }
    
    @Override
    public Page<Admin> getAdminListInternal(Integer page, Integer limit) {
        Page<Admin> pageUtil = new Page<>(page, limit);
        return this.page(pageUtil);
    }
    
    @Override
    public Page<Admin> searchAdminInternal(String username, Integer page, Integer limit) {
        Page<Admin> pageUtil = new Page<>(page, limit);
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.like("username", username);
        return this.page(pageUtil, wrapper);
    }
    
    @Override
    public boolean updateAdminStatusInternal(Integer id, Integer status) {
        Admin admin = this.getById(id);
        if (admin != null) {
            admin.setStatus(status);
            return this.updateById(admin);
        }
        return false;
    }
}